package cn.caishen.report.common.utils;

import java.net.URL;
import java.util.*;


/**
 * @author LB
 */
public class StringUtil {

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    public static boolean objectIsEmpty(Object o) {
        if (Objects.isNull(o)) {
            return true;
        } else {
            return isEmpty(o.toString());
        }
    }

    public static boolean objectIsNotEmpty(Object o) {
        return !objectIsEmpty(o);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String listToString(List list, char separator) {
        return org.apache.commons.lang3.StringUtils.join(list.toArray(), separator);
    }

    public static String listToString(List list) {
        return listToString(list, ',');
    }

    /**
     * 去掉list中的null值
     *
     * @param oldList
     * @param <T>
     * @return
     */
    public static <T> List<T> listRemoveNull(List<? extends T> oldList) {
        // 临时集合
        List<T> listTemp = new ArrayList();
        for (int i = 0; i < oldList.size(); i++) {
            // 保存不为空的元素
            if (oldList.get(i) != null) {
                listTemp.add(oldList.get(i));
            }
        }
        return listTemp;
    }

    /**
     * 去掉list中的null值，和上面的一样，选一种就可以
     *
     * @param oldList
     * @param <T>
     * @return
     */
    public static <T> List<T> listRemoveNull2(List<? extends T> oldList) {
        oldList.removeAll(Collections.singleton(null));
        return (List<T>) oldList;
    }

    /**
     * 拼接字符串
     *
     * @param s
     * @param appendStr
     * @param split
     * @return
     */
    public static String StringAdd(String s, String appendStr, String split) {
        if (isNotEmpty(appendStr)) {
            if ("".equals(s)) {
                s = appendStr;
            } else {
                if (isNotEmpty(split)) {
                    s = s + split;
                }
                s = s + appendStr;
            }
        }
        return s;
    }

    public static String StringAdd(StringBuilder stringBuilder, String appendStr, String split) {
        if (isNotEmpty(appendStr)) {
            if (stringBuilder.length() == 0) {
                stringBuilder.append(appendStr);
            } else {
                if (isNotEmpty(split)) {
                    stringBuilder.append(split);
                }
                stringBuilder.append(appendStr);
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 拼接字符串
     *
     * @param s
     * @param appendStr
     * @return
     */
    public static String StringAdd(String s, String appendStr) {
        return StringAdd(s, appendStr, ",");
    }

    /**
     * 判断是否是数字
     *
     * @param s
     * @return
     */
    public final static boolean isNumeric(String s) {
        if (s != null && !"".equals(s.trim())) {
            return s.matches("^[0-9]*$");
        } else {
            return false;
        }
    }


    public static String getFileExtension(URL extUrl) {
        //URL: "http://photosaaaaa.net/photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg"
        // String filename = "";
        //PATH: /photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg

        String extension = "";
        String path = extUrl.getPath();
        //Checks for both forward and/or backslash
        //NOTE:**While backslashes are not supported in URL's
        //most browsers will autoreplace them with forward slashes
        //So technically if you're parsing an html page you could run into
        //a backslash , so i'm accounting for them here;
        String[] pathContents = path.split("[\\\\/]");
        if (pathContents != null) {
            int pathContentsLength = pathContents.length;
            //System.out.println("Path Contents Length: " + pathContentsLength);
//            for (int i = 0; i < pathContents.length; i++) {
//                System.out.println("Path " + i + ": " + pathContents[i]);
//            }
            //lastPart: s659629384_752969_4472.jpg
            String lastPart = pathContents[pathContentsLength - 1];
            String[] lastPartContents = lastPart.split("\\.");
            if (lastPartContents != null && lastPartContents.length > 1) {
                int lastPartContentLength = lastPartContents.length;
                // System.out.println("Last Part Length: " + lastPartContentLength);
                //filenames can contain . , so we assume everything before
                //the last . is the name, everything after the last . is the
                //extension
                String name = "";
                for (int i = 0; i < lastPartContentLength; i++) {
                    // System.out.println("Last Part " + i + ": "+ lastPartContents[i]);
                    if (i < (lastPartContents.length - 1)) {
                        name += lastPartContents[i];
                        if (i < (lastPartContentLength - 2)) {
                            name += ".";
                        }
                    }
                }
                extension = lastPartContents[lastPartContentLength - 1];
                // filename = name + "." +extension;
                // System.out.println("Name: " + name);
                // System.out.println("Extension: " + extension);
                // System.out.println("Filename: " + filename);
            }
        }
        return extension;
    }

    /**
     * 去除重复数据
     * 由于Set的无序性，不会保持原来顺序
     *
     * @param list
     */
    public static List<LbMap> listDistinct(List<LbMap> list) {
        final boolean isNull = null != list && list.size() > 0;
        List<LbMap> distinctList = new ArrayList<>();
        if (isNull) {
            Set<LbMap> set = new HashSet<LbMap>(list);
            distinctList.addAll(set);
        }
        return distinctList;
    }

    /**
     * 去除重复数据，用于 List<Integer>
     *
     * @param list
     * @return
     */
    public static List<Integer> listIntDistinct(List<Integer> list) {
        final boolean isNull = null != list && list.size() > 0;
        List<Integer> distinctList = new ArrayList<>();
        if (isNull) {
            Set<Integer> set = new HashSet<Integer>(list);
            distinctList.addAll(set);
        }
        return distinctList;
    }


    /**
     * string数组转integer数组
     *
     * @param strings
     * @return
     */
    public static Integer[] str2Int(String[] strings) {
        List<Integer> list = new ArrayList<>();
        for (String s : strings) {
            if (StringUtil.isEmpty(s)) {
                list.add(0);
            } else {
                list.add(Integer.valueOf(s));
            }
        }
        return list.toArray(new Integer[list.size()]);
    }

    public static List<String> str2List(String[] strings) {
        return new ArrayList<>(Arrays.asList(strings));
    }

    public static String changeFileName(String fileName, String changeName) {
        if (StringUtil.isEmpty(fileName)) {
            return "";
        }

        if (StringUtil.isEmpty(changeName)) {
            return fileName;
        }

        if (!fileName.contains(".")) {
            return fileName + changeName;
        }

        int position = fileName.lastIndexOf(".");
        String begin = fileName.substring(0, position);
        String end = fileName.substring(position);
        String nowTime = DateUtil.getNowTime2File();

        return begin + changeName + nowTime + end;
    }

    public static void main(String[] args) {
        String s = changeFileName("124.exe", "3333");
        System.out.println(s);
    }
}
